package edu.hitsz.aircraft;

import edu.hitsz.application.ImageManager;
import edu.hitsz.application.Main;
import edu.hitsz.bullet.AbstractBullet;
import edu.hitsz.strategyshoot.Context;
import edu.hitsz.strategyshoot.SpreadShoot;
import edu.hitsz.strategyshoot.StraightShoot;
import edu.hitsz.strategyshoot.StrategyShoot;

import java.util.List;
import java.util.Stack;

import static java.lang.Thread.sleep;

/**
 * 英雄飞机，游戏玩家操控
 * @author hitsz
 */
public class HeroAircraft extends AbstractAircraft{

    private volatile static HeroAircraft singleHero;

    /**
     * 火力道具的持续时间
     */
    private int time = 10000;
    //定义一个存放英雄机子弹数量的栈
    private Stack<Integer> shootNumStack = new Stack<>();
    //定义一个存放英雄机射击方式的栈
    private Stack<StrategyShoot> shootStack = new Stack<>();

    /**
     * @param locationX 英雄机位置x坐标
     * @param locationY 英雄机位置y坐标
     * @param speedX 英雄机射出的子弹的基准速度（英雄机无特定速度）
     * @param speedY 英雄机射出的子弹的基准速度（英雄机无特定速度）
     * @param hp    初始生命值
     */
    private HeroAircraft(int locationX, int locationY, int speedX, int speedY, int hp,int shootNum,int power,int direction) {
        super(locationX, locationY, speedX, speedY, hp);
        this.shootNum = shootNum;
        this.power = power;
        this.direction = direction;
        shootNumStack.push(1);
        StraightShoot straightShoot = new StraightShoot();
        shootStack.push(straightShoot);
        this.context = new Context(straightShoot);
    }

    public static HeroAircraft creatHeroAircraft(){
        if (singleHero == null){
            synchronized (HeroAircraft.class){
                if (singleHero == null){
                    singleHero = new HeroAircraft(Main.WINDOW_WIDTH / 2,Main.WINDOW_HEIGHT - ImageManager.HERO_IMAGE.getHeight(),0,0,10000,1,30,-1);
                }
            }
        }
        return singleHero;
    }

    @Override
    public void forward() {
        // 英雄机由鼠标控制，不通过forward函数移动
    }

    @Override
    /**
     * 通过射击产生子弹
     * @return 射击出的子弹List
     */
    public List<AbstractBullet> shoot() {
        this.shootNum = shootNumStack.peek();
        this.context.setStrategyShoot(shootStack.peek());
        return context.shoot(this,power,direction,shootNum);
    }

    public void changeshoot(){
        Runnable pop = () -> {
            try {
                sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            shootNumStack.pop();
            shootStack.pop();
        };
        if (this.shootNum < 5) {
            if (this.shootNum == 1) {
                this.shootStack.push(new StraightShoot());
                this.shootNumStack.push(2);
            } else if (this.shootNum == 2){
                this.shootStack.push(new SpreadShoot());
                this.shootNumStack.push(3);
            } else {
                this.shootStack.push(new SpreadShoot());
                this.shootNumStack.push(5);
            }
            new Thread(pop).start();
        }
    }
}
